package com.eshop.controller.user;

import com.eshop.pojo.Good;
import com.eshop.pojo.RespBean;
import com.eshop.pojo.RespPageBean;
import com.eshop.pojo.User;
import com.eshop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户基本信息管理相关")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    //分页获取商品列表
    @ApiOperation("分页获取用户信息列表")
    @GetMapping("/")
    public RespPageBean getUserListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "8") Integer size, User user) {
        return userService.getUserListByPage(currentPage, size,user);
    }

    @ApiOperation(value = "通过账号查找用户")
    @GetMapping("/{name}")
    public User queryUserListByName(@PathVariable String name){
        User user = userService.queryUserByName(name);
        return user;
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping("/")
    public RespBean updateUser(@RequestBody User user){
        if (userService.updateById(user)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/{id}")
    public RespBean deteleUser(@PathVariable Integer id){
        if (userService.removeById(id)){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "禁用用户")
    @PutMapping("/ban/{id}")
    public RespBean banUser(@PathVariable Integer id){
        if (userService.banUserById(id)){
            return RespBean.success("禁用成功！重新登陆后生效！");
        }
        return RespBean.error("禁用失败！重新登陆后生效！");
    }

    @ApiOperation(value = "解禁用户")
    @PutMapping("/reuse/{id}")
    public RespBean reuseUser(@PathVariable Integer id){
        if (userService.reuseUserById(id)){
            return RespBean.success("解禁成功！重新登陆后生效！");
        }
        return RespBean.error("解禁失败！重新登陆后生效！");
    }
}
